package sparrowGui
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.utils.getTimer;
	
	import sparrowGui.skin.NumericStepperSkin;
	import sparrowGui.utils.PubFun;

	/**
	 * 数字单步增减组件
	 * @author Pelephone
	 */	
	public class NumericStepper extends Component
	{
		static public const INPUT_NUM_CHANGE:String = "input_num_change";
		
		private var clickTime:int;			//暂存按下时间点
		private var delayTime:int = 500;	//按下后延时一定时间快速加时间
		private var diffNum:int = 0;
		
		public function NumericStepper(argSkin:Sprite=null)
		{
			_skin = argSkin || new NumericStepperSkin();
			PubFun.parseObjToNew(skin,this);
			super();
		}
		
		override protected function init():void
		{
			addChild(skin);
			txt_inputNum.restrict = '0-9';
			
			txt_inputNum.addEventListener(Event.CHANGE,onEvt);
			btn_increase.addEventListener(MouseEvent.CLICK,onEvt);
			btn_decrease.addEventListener(MouseEvent.CLICK,onEvt);
			
			btn_increase.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownUp);
			btn_decrease.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDownUp);
			
			this.addEventListener(Event.ADDED_TO_STAGE,addToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE,removeToStage);
		}
		
		private function addToStage(e:Event):void
		{
			stage.addEventListener(MouseEvent.MOUSE_UP,onMouseDownUp);
			stage.addEventListener(Event.MOUSE_LEAVE,onMouseDownUp);
		}
		
		private function removeToStage(e:Event):void
		{
			stage.removeEventListener(MouseEvent.MOUSE_UP,onMouseDownUp);
			stage.removeEventListener(Event.MOUSE_LEAVE,onMouseDownUp);
		}
		
		private function onEvt(e:Event):void
		{
			switch(e.target){
				case btn_increase:
					txt_inputNum.text = String(int(txt_inputNum.text) + 1);
					break;
				case btn_decrease:
					txt_inputNum.text = String(int(txt_inputNum.text) - 1);
					break;
				case txt_inputNum:
					dispatchEvent(new Event(INPUT_NUM_CHANGE));
					break;
			}
		}
		
		// 鼠标按下放开
		private function onMouseDownUp(e:Event):void
		{
			switch(e.type){
				case MouseEvent.MOUSE_DOWN:
					clickTime = getTimer();
					
					if(e.target == btn_increase) diffNum = 1;
					else if(e.target == btn_decrease) diffNum = -1;
					this.addEventListener(Event.ENTER_FRAME,onEnterEvt);
					break;
				case Event.MOUSE_LEAVE:
				case MouseEvent.MOUSE_UP:
					diffNum = 0;
					this.removeEventListener(Event.ENTER_FRAME,onEnterEvt);
					break;
			}
		}
		
		private function onEnterEvt(e:Event):void
		{
			if((getTimer()-clickTime)>delayTime && diffNum!=0){
				txt_inputNum.text = String(int(txt_inputNum.text) + diffNum);
			}
		}
		
		override public function dispose():void
		{
			txt_inputNum.removeEventListener(Event.CHANGE,onEvt);
			btn_increase.removeEventListener(MouseEvent.CLICK,onEvt);
			btn_decrease.removeEventListener(MouseEvent.CLICK,onEvt);
			
			btn_increase.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDownUp);
			btn_decrease.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseDownUp);
			
			this.removeEventListener(Event.ADDED_TO_STAGE,addToStage);
			this.removeEventListener(Event.REMOVED_FROM_STAGE,removeToStage);
			
			removeToStage(null);
			
			super.dispose();
		}
		
		public var txt_inputNum:TextField;		//输入文本,只能输入数字
		public var btn_increase:DisplayObject;	//增加按钮
		public var btn_decrease:DisplayObject;	//减少按钮
	}
}